package com.jcl.reflect.util;

import cn.hutool.core.util.ReflectUtil;
import com.jcl.reflect.TreeTest;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class MyReflectUtil {
    private static final AtomicInteger i = new AtomicInteger(0);

    public static LinkedHashMap<Integer, Class> getGenericType(Class c, LinkedHashMap<Integer, Class> map) {
        Field[] fields = ReflectUtil.getFields(c);
        for (Field field : fields) {
            // 当前属性里有list的
            if (field.getType() == List.class) {
                field.setAccessible(true);
                Type genericType = field.getGenericType();
                if (genericType instanceof ParameterizedType) {
                    i.incrementAndGet();
                    ParameterizedType pt = (ParameterizedType) genericType;
                    // 得到泛型里的class类型对象
                    Class<?> actualTypeArgument = (Class<?>) pt.getActualTypeArguments()[0];
                    map.put(i.get(), actualTypeArgument);
                    getGenericType(actualTypeArgument, map);
                }
                break;
            }
        }
        return map;
    }
}
